<template>
  <div class="ref">
    <form action="/">
      <van-search placeholder="" v-model="sval" show-action @search="onSearch" @cancel="onCancel" style="position:fixed;top:0vh;left:0vw;width:100vw;z-index:1" />
    </form>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh" style="margin-top:8vh;margin-bottom:8vh;">
      <van-list v-model="loading" :finished="finished" finished-text="已加载全部数据" @load="onLoad">
        <van-cell v-for="item in list" :key="item.id" :value="item.value" @click="reBack(item)">
          <template #title>
            <span>{{ item.title }}</span><br>
            <span style="font-size: 12px;color: #969799;" v-html="item.label"></span>
            <img v-if="item['sfzdys']=='1'" src="@/assets/img_important2.png" style="width:40px;position:absolute;right:10px;bottom:0">
          </template>

        </van-cell>

      </van-list>
    </van-pull-refresh>
    <!-- <div class="add-image">
      <img :src="require('@/assets/add.png')" @click="openNew" />
    </div> -->
    <!--    <van-tabbar>-->
    <!--      <van-tabbar-item @click="openNew">新增</van-tabbar-item>-->
    <!--    </van-tabbar>-->

  </div>
</template>

<script>
import Vue from 'vue'
import { Search, List, Cell, PullRefresh, Toast, Tabbar, TabbarItem, Popup } from 'vant'
import qs from 'qs'
Vue.use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup)
import { insertPointMixins } from '@/mixins/insterPoint.js'
export default {
  name: 'ref',
  mixins: [insertPointMixins],
  data () {
    return {
      sval: this.$route.params.conditionVal,
      list: [],
      loading: false,
      finished: false,
      isLoading: false,
      startline: 1,
      rebackobject: { reBackflag: false },
      fieldref: this.$route.params.fieldref,
      bo: this.$route.params.bo,
      stitle: this.$route.params.stitle
    }
  },
  created () {
    document.title = this.stitle
  },
  methods: {
    onLoad () {
      console.log(this.$route.params,123123)
      let data = {
        'ref_object': this.$route.params.bo,
        'ref_toobject': this.$route.params.tobo,
        'ref_tofieldas': this.$route.params.field,
        'ref_inview': this.$route.params.conditionValHidden,
        'view_page': this.startline,
        'view_id': this.$route.params.viewid,
        'ref_classname': this.$route.params.classname,
        'ref_params': this.$route.params.params,
        'ref_conditionval': this.sval
      }
      //console.log(this.sval)
      data.ref_inview = data.ref_inview.replace(':in:', ':in:' + this.sval)
      //console.log('data.ref_inview', data.ref_inview)
      this.axios({
        method: 'post',
        url: '/api/ref.listnew.do',
        data: qs.stringify(data)
      }).then(res => {
        //console.log(res)
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          } else {
            this.$router.go(-1)
          }
        } else {
          let rsdata = res.data.jsondata.outdata
          for (let i = 0; i < rsdata?.length; i++) {
            this.list.push(rsdata[i])
          }

          




          if (rsdata == null || rsdata.length === 0) {
            this.loading = false
            this.finished = true
          } else {
            this.startline = this.startline + 1
            this.loading = false
          }
        }
      }).catch(res => {
        this.reserror(res)
      })
    },
    onRefresh () {
      this.list = []
      this.startline = 1
      this.isLoading = false
      this.loading = true
      this.finished = false
      this.onLoad()
    },
    openNew () {
      this.$router.replace({ name: 'new', query: { stitle: this.stitle, VNK: 'new' + new Date().getTime(), idobject: this.bo, newflag: 'refnew', reBackflag: this.rebackobject } })
    },
    onSearch () {
      this.list = []
      this.startline = 1
      this.onLoad()
    },
    onCancel () {
      this.sval = ''
      this.list = []
      this.startline = 1
      // this.onLoad()
      this.$router.go(-1)
    },
    async reBack (item) {
      // if(this.$route.params.classname){
      //   const res = await this.getRefSelect(item.id)

      // }else{
      //   this.$store.commit('refReBack', { ref_id: item.id, ref_show: item[this.fieldref], ref_field: this.$route.params.field, ref_item: item })
      //   this.$router.go(-1)
      // }
      const res = await this.getRefSelect(item.id)
      // console.log("item",item)
      // console.log("this.fieldref",this.fieldref)
      // 回写
      const outdata = res.data?.jsondata?.outdata

      // this.$toast(item.id + '-->' + item.title)
      const storeRefObj = {
        ref_id: item.id,
        ref_show: item[this.fieldref] || outdata[`${this.$route.params.field}_ref`],
        ref_field: this.$route.params.field,
        ref_item: item
      }
      console.log(storeRefObj,'storeRefObjstoreRefObjstoreRefObj')
      if (Object.getOwnPropertyNames(outdata).length > 2) {
        storeRefObj.ref_map = outdata
        console.log("storeRefObj",storeRefObj)
        storeRefObj.ref_toobject = this.$route.params.tobo

      }
      console.log(storeRefObj,12345555)
      // //console.log('refBack: ', item.id, this.fieldref, item[this.fieldref], this.$route.params.field, item)
      this.$store.commit('refReBack', storeRefObj)
      this.$router.go(-1)
    },
     getRefSelect (id) {
      const data = {
        ref_object: this.$route.params.bo,
        ref_id: id,
        ref_toobject: this.$route.params.tobo,
        ref_tofieldas: this.$route.params.field,
        ref_tofield: this.$route.params.field,
        ref_classname: this.$route.params.classname,
        ref_params: this.$route.params.params
      }
      return this.axios({
        method: 'post',
        url: '/api/ref.select.do',
        data: qs.stringify(data)
      })
    }

  },
  activated () {
    if (this.$store.state.ref_show === 'true') {
      this.$store.state.ref_show = ''
      this.$store.state.ref_id = ''
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.fieldgroup_title {
  text-align: left;
  margin: 0px;
  padding: 35px 15px 15px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
}

.van-cell .van-cell__title {
  flex: 2 !important;
}

.van-cell .van-cell__value {
  flex: 1 !important;
}

.add-image {
  position: fixed; /* 固定位置 */
  right: 5px; /* 距离右边的距离 */
  bottom: 80px; /* 距离底部的距离 */
  z-index: 1000; /* 确保在页面上显示 */
}

.add-image img {
  width: 75px; /* 图片的宽度 */
  height: auto; /* 高度自动调整 */
  border-radius: 50%; /* 可选：将图片设置为圆形 */
}
</style>
